package com.fzd.code.file.storage;

import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class DefaultUrlRuleParser implements IUrlRuleParser {

    public void parse(StorageConfig storageConfig, Source source) {
        String[] rules = storageConfig.getUrlRule().split("/");
        for(int i=0;i<rules.length;i++){
            String rule=rules[i];
            DefaultUrlRule r = DefaultUrlRule.valueOf(rule);
            switch (r){
                case $MODULE$:
                    storageConfig.setUrlRule(
                            storageConfig.getUrlRule().replace(DefaultUrlRule.$MODULE$.name(),storageConfig.getModule())
                    );
                    break;
                case $YEAR_MONTH$:
                    storageConfig.setUrlRule(
                            storageConfig.getUrlRule().replace(DefaultUrlRule.$YEAR_MONTH$.name(),new SimpleDateFormat("yyyyMM").format(new Date()))
                    );
                    break;
                case $UUID$:
                    storageConfig.setUrlRule(
                            storageConfig.getUrlRule().replace(DefaultUrlRule.$UUID$.name(), UUID.randomUUID().toString().replaceAll("-",""))
                    );
                    break;
            }
        }
    }
}
